<?php

class ServiceController extends Controller {

    public function actionIndex() {
        $data = parent::$pageData;
        $this->pageTitle = $data['pageTitle'] . ' — Моби03';
        Yii::app()->clientScript->registerMetaTag('', 'description');
        Yii::app()->clientScript->registerMetaTag($data['pageTitle'], 'keywords');
        $criteria = new CDbCriteria();
        $criteria->select = '"pageTitle", "pageUrl", "pageImages" ';
        $criteria->compare('"pageActive"', 1);
        $criteria->compare('"pageType"', 'servcenter');
        $criteria->order = '"pageSort", "pageTitle"';
//		$count = Page::model()->count($criteria);
//		$pages = new CPagination($count);
//		$pages->pageSize = 18;
//		$pages->applyLimit($criteria);
        $models = Page::model()->findAll($criteria);
        $this->render('index', array('models' => $models));
    }

    public function actionView() {
        $data = parent::$pageData;

        foreach (Yii::app()->params['regions'] as $key => $value) {
            $regions[] = $value['genitive'];
        }
        $regions = implode(', ', $regions);
        $replaceBrand = array('mts' => 'МТС', 'Sony-ericsson' => 'sony', 'sony-ericsson'=>'sony', '3q-qoo'=>'3Q');
        $title = isset($replaceBrand[Controller::$pageData['pageTitle']]) ? $replaceBrand[Controller::$pageData['pageTitle']] : Controller::$pageData['pageTitle'];
        $sql = "SELECT p.\"pageId\", p.\"pageTitle\", m.\"pageRussianName\" FROM {{pages}} p LEFT JOIN {{metas}} m ON p.\"pageId\" = m.\"itemId\" AND m.\"itemType\"='page' WHERE LOWER(p.\"pageTitle\") = '" . strtolower($title) . "' AND p.\"pageActive\" = 1 AND p.\"pageType\" = 'brand' ORDER BY \"pageSort\" DESC";
        $parentBrand = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryRow();
        $replaces = array('эпл ай воч' => 'эпл', 'эпл айпад' => 'эпл', 'айфон' => 'эпл', 'макбук' => 'эпл', 'эппл аймак' => 'эпл', 'эпл айпод' => 'эпл');

        $parentBrand['pageRussianName'] = isset($replaces[$parentBrand['pageRussianName']]) ? $replaces[$parentBrand['pageRussianName']] : $parentBrand['pageRussianName'];
        if (empty($parentBrand['pageRussianName']))
            $parentBrand['pageRussianName'] = $title;
        $this->pageTitle = 'Моби03 — сервисный центр ' . ucfirst(Controller::$pageData['pageTitle']) . ' (' . $parentBrand['pageRussianName'] . ') в ' . $regions . '.';
        Yii::app()->clientScript->registerMetaTag('Федеральный сервис центр Моби03 занимается ремонтом техники ' . ucfirst(Controller::$pageData['pageTitle']) . ' в ' . $regions . '.', 'description');
        Yii::app()->clientScript->registerMetaTag('все сервис центры ' . ucfirst(Controller::$pageData['pageTitle']), 'keywords');

        $this->render('view', array('data' => $data));
    }

}
